package dj.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import dj.Constant;
import dj.action.base.BaseAction;
import dj.model.OperateLog;
import dj.pageModel.PoperateLog;
import dj.pageModel.Puser;
import dj.service.OperateLogService;

@Action("operatelogAction")
public class OperateLogAction extends BaseAction implements ModelDriven<PoperateLog> {

	private PoperateLog pl = new PoperateLog();

	private OperateLogService opreateLogService;

	public PoperateLog getModel() {
		return pl;
	}

	public OperateLogService getOpreateLogService() {
		return opreateLogService;
	}

	@Autowired
	public void setOpreateLogService(OperateLogService opreateLogService) {
		this.opreateLogService = opreateLogService;
	}

	public void getOperateLog() {
		Map<String, Object> operateLogMap = new HashMap<String, Object>();
		try {
			long operateLogNum = opreateLogService.getOperateLogNum();
			List<OperateLog> operateLogs = opreateLogService.getOperateLogPagination();

			List<PoperateLog> operatelog_list = new ArrayList<PoperateLog>();
			if (null != operateLogs && operateLogs.size() > 0) {
				for (OperateLog operatelog : operateLogs) {
					PoperateLog poperatelog = new PoperateLog();
					BeanUtils.copyProperties(operatelog, poperatelog);
					operatelog_list.add(poperatelog);
				}
			}

			operateLogMap.put("total", operateLogNum);
			operateLogMap.put("rows", operatelog_list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.writeToJson(operateLogMap);
	}
	
	public void getCompanyOperageLog() {
		Puser loginUser = (Puser) ServletActionContext.getRequest().getSession().getAttribute(Constant.USER_SESSION);
		Map<String, Object> operateLogMap = new HashMap<String, Object>();
		try {
			long operateLogNum = opreateLogService.getCompanyOperateLogNum(loginUser);
			List<OperateLog> operateLogs = opreateLogService.getCompanyOperateLogPagination(loginUser);

			List<PoperateLog> operatelog_list = new ArrayList<PoperateLog>();
			if (null != operateLogs && operateLogs.size() > 0) {
				for (OperateLog operatelog : operateLogs) {
					PoperateLog poperatelog = new PoperateLog();
					BeanUtils.copyProperties(operatelog, poperatelog);
					operatelog_list.add(poperatelog);
				}
			}

			operateLogMap.put("total", operateLogNum);
			operateLogMap.put("rows", operatelog_list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.writeToJson(operateLogMap);
		
	}
	
	public void getOperateDetail() {
		OperateLog ol = opreateLogService.getOperateById(pl.getId());
		PoperateLog plg = new PoperateLog();
		if(null != ol) {
			BeanUtils.copyProperties(ol, plg);
		}
		
		super.writeToJson(plg);
	}

}
